#ifndef SRVCONNECTION_H_
#define SRVCONNECTION_H_

//boost headers
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/noncopyable.hpp>
#include <boost/signal.hpp>
#include <boost/signals.hpp>
#include <boost/signals/connection.hpp>
#include <boost/thread/mutex.hpp>

//std headers
#include <vector>
#include <queue>
#include <string>

//project specific headers
#include <DataContainer.h>
#include "../Validator/Scheme.h"



#define BOOST_REGEX_NO_LIB


class ConnectionContainer
{
	public :
		PComunicator::DataContainer data;
		boost::asio::ip::address clientAddress;
		std::string port;
};


/*! 
* SRVConnection trieda
* 
* Trieda predstavuje jedno pripojenie z strany klienta na samotny server
* Cielom triedy je obstaravanie vsetkych operacii spojenych s obsluhov klienta.
* SRVConnection je nekopirovatelna !
*/
class SRVConnection : public boost::enable_shared_from_this<SRVConnection>, private boost::noncopyable
{
	public:
		/*!
		 * Konstruktor triedy
		 * \param io_service objekt typu boost::asio::io_service
		 * \param handler    objekt typu RequestHandler
		 */
		explicit SRVConnection(boost::asio::io_service &io_service,std::queue<boost::shared_ptr<ConnectionContainer> > *theReceived);

		/*!
			 Navrati socket patriaci danemu spojeniu
			 \return boost::asio::ip::tcp::socket
		 */
		boost::asio::ip::tcp::socket& GetSocket();

		/*!
			 Spusti prvu asynchronnu operaciu pre dane spojenie
		*/
		void Start();
		

		

	private:

		//////////////////////////////////////////////////////////////////////////
		//							MEMBER FUNCTIONS
		//////////////////////////////////////////////////////////////////////////
		/*!
		* Obsluha ukoncenia operacie citania
		*/
		void handle_read(const boost::system::error_code &er,std::size_t bytes_transfered);

		/*!
		* Obsluha ukoncenia operacie zapisu
		*/
		void handle_write(const boost::system::error_code &er);

		//////////////////////////////////////////////////////////////////////////
		//							MEMBER VARIABLES
		//////////////////////////////////////////////////////////////////////////

		boost::asio::io_service::strand myStrand;
		boost::asio::ip::tcp::socket mySocket;
		boost::array<char,8192> myLocalBuffer; 
		std::vector<unsigned char> myOveralBuffer;
		std::size_t myReceivingSize;
		bool myReceiving;
		boost::mutex myMutex;
		PComunicator::DataContainer myRequest;		//prichadzajuce data
		
		//std::queue<boost::shared_ptr<PComunicator::DataContainer> > *myReceived;
		std::queue<boost::shared_ptr<ConnectionContainer> > *myReceived;
};

/*!
 * definicia zdielaneho smernika na jedno spojenie
 */
typedef boost::shared_ptr<SRVConnection> connection_ptr;


#endif
